package com.ruoyi.basebill.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.basebill.DTO.QqjchEntryInfo;
import com.ruoyi.basebill.DTO.QqjchInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.basebill.domain.WfzChqqjentry;
import com.ruoyi.basebill.mapper.WfzChqqjMapper;
import com.ruoyi.basebill.domain.WfzChqqj;
import com.ruoyi.basebill.service.IWfzChqqjService;

/**
 * 冲回全勤奖Service业务层处理
 * 
 * @author xiefeng
 * @date 2022-12-19
 */
@Service
public class WfzChqqjServiceImpl implements IWfzChqqjService 
{
    @Autowired
    private WfzChqqjMapper wfzChqqjMapper;

    /**
     * 查询冲回全勤奖
     * 
     * @param fid 冲回全勤奖主键
     * @return 冲回全勤奖
     */
    @Override
    public WfzChqqj selectWfzChqqjByFid(Long fid)
    {
        return wfzChqqjMapper.selectWfzChqqjByFid(fid);
    }

    /**
     * 查询冲回全勤奖列表
     * 
     * @param wfzChqqj 冲回全勤奖
     * @return 冲回全勤奖
     */
    @Override
    public List<WfzChqqj> selectWfzChqqjList(WfzChqqj wfzChqqj)
    {
        return wfzChqqjMapper.selectWfzChqqjList(wfzChqqj);
    }

    /**
     * 新增冲回全勤奖
     * 
     * @param wfzChqqj 冲回全勤奖
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWfzChqqj(WfzChqqj wfzChqqj)
    {
        int rows = wfzChqqjMapper.insertWfzChqqj(wfzChqqj);
        insertWfzChqqjentry(wfzChqqj);
        return rows;
    }

    /**
     * 修改冲回全勤奖
     * 
     * @param wfzChqqj 冲回全勤奖
     * @return 结果
     */
    @Transactional
    @Override
    public int updateWfzChqqj(WfzChqqj wfzChqqj)
    {
        wfzChqqjMapper.deleteWfzChqqjentryByFparentid(wfzChqqj.getFid());
        insertWfzChqqjentry(wfzChqqj);
        return wfzChqqjMapper.updateWfzChqqj(wfzChqqj);
    }

    /**
     * 批量删除冲回全勤奖
     * 
     * @param fids 需要删除的冲回全勤奖主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWfzChqqjByFids(Long[] fids)
    {
        wfzChqqjMapper.deleteWfzChqqjentryByFparentids(fids);
        return wfzChqqjMapper.deleteWfzChqqjByFids(fids);
    }

    /**
     * 删除冲回全勤奖信息
     * 
     * @param fid 冲回全勤奖主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWfzChqqjByFid(Long fid)
    {
        wfzChqqjMapper.deleteWfzChqqjentryByFparentid(fid);
        return wfzChqqjMapper.deleteWfzChqqjByFid(fid);
    }

    /**
     * 新增冲回全勤奖分录信息
     * 
     * @param wfzChqqj 冲回全勤奖对象
     */
    public void insertWfzChqqjentry(WfzChqqj wfzChqqj)
    {
        List<WfzChqqjentry> wfzChqqjentryList = wfzChqqj.getWfzChqqjentryList();
        Long fid = wfzChqqj.getFid();
        if (StringUtils.isNotNull(wfzChqqjentryList))
        {
            List<WfzChqqjentry> list = new ArrayList<WfzChqqjentry>();
            for (WfzChqqjentry wfzChqqjentry : wfzChqqjentryList)
            {
                wfzChqqjentry.setFparentid(fid);
                list.add(wfzChqqjentry);
            }
            if (list.size() > 0)
            {
                wfzChqqjMapper.batchWfzChqqjentry(list);
            }
        }
    }

    /**
     * 导入全勤奖冲回
     */
    @Override
    public String importQqjch(WfzChqqj wfzChqqj) {
        QqjchInfo qqjchInfo = new QqjchInfo();
        String fnumber = wfzChqqj.getFnumber();//单据编号
        String fcompany = wfzChqqj.getFcompany();//组织（公司）
        String fperiod = wfzChqqj.getFperiod();//薪酬期间
        String fvourtype = wfzChqqj.getFvourtype();//凭证字

        qqjchInfo.setNumber(fnumber);
        qqjchInfo.setCompany(fcompany);
        qqjchInfo.setPeriod(fperiod);
        qqjchInfo.setVourType(fvourtype);
        qqjchInfo.setSourcebillid(String.valueOf(wfzChqqj.getFid()));

        List<WfzChqqjentry> wfzChqqjentryList = wfzChqqj.getWfzChqqjentryList();
        ArrayList<QqjchEntryInfo> entry = new ArrayList<>();
        //组装分录数据
        for (int i = 0; i < wfzChqqjentryList.size(); i++) {
            WfzChqqjentry wfzChqqjentry = wfzChqqjentryList.get(i);
            String fcostnumber = wfzChqqjentry.getFcostnumber();//成本中心
            BigDecimal fqqj = wfzChqqjentry.getFqqj();//全勤奖
            String fcostcompany = wfzChqqjentry.getFcostcompany();//成本中心所属财务组织
            String fjzcompany = wfzChqqjentry.getFjzcompany();//记账公司
            String fjsfs = wfzChqqjentry.getFjsfs();//组织间结算方式
            String fcbzxxz = wfzChqqjentry.getFcbzxxz();//成本中心属性
            String fvourtype1 = wfzChqqjentry.getFvourtype();//凭证字

            QqjchEntryInfo qqjchEntryInfo = new QqjchEntryInfo();
            qqjchEntryInfo.setCostCenter(fcostnumber);
            qqjchEntryInfo.setQqj(fqqj);
            qqjchEntryInfo.setCostcompany(fcostcompany);
            qqjchEntryInfo.setJzcompany(fjzcompany);
            qqjchEntryInfo.setJsfs(fjsfs);
            qqjchEntryInfo.setCbzxxz(fcbzxxz);
            qqjchEntryInfo.setVourType(fvourtype1);

            entry.add(qqjchEntryInfo);
        }

        qqjchInfo.setEntry(entry);

        //转为Json
        String json = JSONObject.toJSONString(qqjchInfo);
        return json;
    }
}
